import json
import tld
import csv

dic = {}
for i in range(1,6):
    with open('../data/api_accessibility/api_version_accessbiliby-'+str(i)+'.txt','r') as f:
        data = json.load(f)
        for j in data:
            for k in range(len(j['visit_status'])):
                # try:
                url = j['visit_status'][k]['visit_url'].strip() ##去掉前后空格
                try:
                    ret = tld.get_fld(url)
                    if ret in dic:
                        dic[ret].add(j['from_api']['url'])
                    else:
                        dic[ret] = set()
                        dic[ret].add(j['from_api']['url'])
                except Exception as e:
                    pass  
                # except Exception as e:
                #     pass          


header_list = ['server','API_number']        
# 以写方式打开文件。注意添加 newline=""，否则会在两行数据之间都插入一行空白。
with open("../output/output1/提供商发布Web_API的个数.csv", mode="w", encoding="utf-8-sig", newline="") as f:                
    # 基于打开的文件，创建 csv.writer 实例
    writer = csv.writer(f)

    # 写入 header。
    # writerow() 一次只能写入一行。
    writer.writerow(header_list)

    for i in dic:
        writer.writerow([i,len(dic[i])])                   
